<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\Order;
use App\Models\User;

class OrderRepository
{
    /**
     * 创建用户订单
     * @param array $params
     * @return mixed
     */
    public function create(array $params)
    {
        return Order::create($params);
    }

    /**
     * 创建用户订单
     * @param array $params
     * @return mixed
     */
    public function insertGetId(array $params)
    {
        return Order::insertGetId($params);
    }

    /**
     * 查找一条订单记录
     * @param $id
     * @return mixed
     */
    public function byId($id)
    {
        return Order::find($id);
    }

    public function byOrderNo($orderNo)
    {
        return Order::where('order_no', $orderNo)->first();
    }

    /**
     * 根据订单号更新订单状态
     * @param $orderNo
     * @param $orderStatus
     * @return mixed
     */
    public function updateOrderStatusByOrderNo($orderNo, $orderStatus)
    {
        return Order::where('order_no', $orderNo)->update(['pay_status' => $orderStatus, 'update_time' => time()]);
    }

    public function getList($request)
    {
        $page = $request->pageNumber - 1;
        $page = $page < 0 ? 0 : $page;
        $limit = $request->pageSize ? $request->pageSize : 50;
        $offset = $page * $limit;
        $query = Order::where(function ($query) use ($request) {
            if (!empty($request->pay_status)) {
                $query->where('pay_status', $request->pay_status);
            }
            if (!empty($request->pay_way)) {
                $query->where('pay_way', $request->pay_way);
            }
            if (!empty($request->keyword)) {
                switch ($request->field) {
                    case 'user_id':
                        $query->where('user_id', $request->keyword);
                        break;
                    case 'order_no':
                        $query->where('order_no', 'like', '%' . $request->keyword . '%');
                        break;
                    case 'idcard_number':
                        $userId = User::where('idcard_number', $request->keyword)->value('user_id');
                        $query->where('user_id', $userId);
                        break;
                    case 'realname':
                        $userId = User::where('realname', $request->keyword)->value('user_id');
                        $query->where('user_id', $userId);
                        break;
                    case 'invited_code':
                        $userId = User::where('invited_code', $request->keyword)->value('user_id');
                        $query->where('user_id', $userId);
                        break;
                }
            }
            if (!empty($request->start_time) && !empty($request->end_time)) {
                $query->whereBetween('create_time', [strtotime($request->start_time), strtotime($request->end_time)]);
            }
        });
        $total = $query->count();
        $result = $query->latest('id')
            ->offset($offset)
            ->limit($limit)
            ->get();
        return ['result' => $result, 'count' => $total];
    }

}